﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using InventoryManagement;
using System.Web.UI.WebControls;

namespace MII_FIF.Layouts.MII_FIF.Master.RackNumber
{
    public partial class AddRackNumber : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Generic.IsUserInGroup(Configuration.MasterGroupName))
            {
                btnSubmit.Click += btnSubmit_Click;
                btnCancel.Click += btnCancel_Click;
                if (Request.QueryString["ID"] != null)
                    ((Label)Header.FindControl("lblHeader")).Text = "Edit Rack Number";
                else
                    ((Label)Header.FindControl("lblHeader")).Text = "Add Rack Number";
                if (!IsPostBack)
                {
                    load();
                }
            }
            else
            {
                Response.Redirect(Generic.GenLink("/"));
            }
        }

        void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect(Generic.GenLink("/_Layouts/MII_FIF/Master/RackNumber/ViewRackNumber.aspx"));
        }

        void btnSubmit_Click(object sender, EventArgs e)
        {
            Rack_Number rack = new Rack_Number();
            lblError.Text = string.Empty;
            if (txtRackNumber.Text.Trim() == string.Empty)
            {
                lblError.Text = "Please insert rack number. <br />";
            }
            else
            {
                rack.Title = txtRackNumber.Text;
                rack.IsDeleted = false;

                if (Request.QueryString["ID"] != null)
                {
                    rack.ID = Guid.Parse(ViewState["ID"].ToString());
                    rack.Select();
                    if (rack.IsDeleted.Value)
                    {
                        lblError.Text += "This data is already deleted. <br />";
                    }
                    else
                    {
                        rack.Title = txtRackNumber.Text;
                        rack.IsDeleted = false;
                        int stat = rack.Update();
                        if (stat > 0)
                            Response.Redirect(Generic.GenLink("/_Layouts/MII_FIF/Master/RackNumber/ViewRackNumber.aspx"));
                        else
                            lblError.Text += "Update Failed";
                    }
                }
                else
                {
                    rack.ID = Guid.NewGuid();
                    int stat = rack.Insert();
                    if (stat > 0)
                        Response.Redirect(Generic.GenLink("/_Layouts/MII_FIF/Master/RackNumber/ViewRackNumber.aspx"));
                    else
                        lblError.Text += "Insert Failed";
                }
            }
        }

        void load()
        {
            Rack_Number rack = new Rack_Number();
            if (Request.QueryString["ID"] != null)
            {
                ViewState["ID"] = Request.QueryString["ID"].ToString();
                rack.ID = Guid.Parse(Request.QueryString["ID"].ToString());
                rack.Select();
                txtRackNumber.Text = rack.Title;
            }

            if (Request.QueryString["ID"] != null)
                ((Label)Header.FindControl("lblHeader")).Text = "Edit Rack Number";
            else
                ((Label)Header.FindControl("lblHeader")).Text = "Add Rack Number";
        }
    }
}
